<head></head>
<body>
<?php

	$graphstr = '';
	$graphstr = "digraph entireTree {\n" . $graphstr;
	
	include_once "model.php";
	
	function render($left, $right) {
		global $graphstr;    
		$left = preg_replace("/(\r|\n)*/", "", $left);
		$graphstr .= '"' . $left . "\" -> \"" . $right . "\";\n";
	}
	
	function helper($model, $left, $right) {
		global $graphstr;
		
		$middle = $model->get_middle($left[NODE_ID], $right[NODE_ID]);
		
		if ($middle != NULL){
			render($middle[NODE_CONTENT], $left[NODE_CONTENT]);
			helper($model, $left, $middle);
			helper($model, $middle, $right);
		}  
	
	}   
	
	
	$first = $model->get_node(1);
	$last = $model->get_node(2);
	
	helper($model, $first, $last);
	
	$graphstr .= "}\n";
	
	
	$handle = fopen("test.dot", "w+");
	fputs($handle, $graphstr);
	fclose($handle);
	
	echo shell_exec('dot -Tsvg -otest.svg test.dot' );
	
	echo "<pre>" . $graphstr . "</pre>";

?>


</body>
                         
